package com.wxzz.elearing.livetelecast.service.impl;

import cn.afterturn.easypoi.cache.manager.IFileLoader;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.wxzz.elearing.livetelecast.entity.LiveRecordCallbackEntity;
import com.wxzz.elearing.livetelecast.entity.LivetelecastRecord;
import com.wxzz.elearing.livetelecast.entity.PlatformLivetelecast;
import com.wxzz.elearing.livetelecast.mapper.LivetelecastRecordMapper;
import com.wxzz.elearing.livetelecast.mapper.PlatformLivetelecastMapper;
import com.wxzz.elearing.livetelecast.service.ILivetelecastRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 直播录播文件 服务实现类
 * </p>
 *
 * @author
 * @since 2021-05-14
 */
@Service
@Slf4j
public class LivetelecastRecordServiceImpl extends ServiceImpl<LivetelecastRecordMapper, LivetelecastRecord> implements ILivetelecastRecordService {

    // 录制开始
    private static final String RECORD_START = "RECORD_START";
    // 开始创建新的录制文件
    private static final String RECORD_NEW_FILE_START = "RECORD_NEW_FILE_START";
    // 录制文件生成完成
    private static final String RECORD_FILE_COMPLETE = "RECORD_FILE_COMPLETE";
    // 录制结束
    private static final String RECORD_OVER = "RECORD_OVER";
    // 录制失败
    private static final String RECORD_FAILED = "RECORD_FAILED";

    @Autowired
    LivetelecastRecordMapper livetelecastRecordMapper;
    @Autowired
    PlatformLivetelecastMapper platformLivetelecastMapper;

    /**
     * 华为云直播回调保存录播资源
     *
     * @param liveRecordCallbackEntity
     * @return
     */
    @Override
    public Boolean saveRecordByCallback(LiveRecordCallbackEntity liveRecordCallbackEntity) {

        log.info("华为云直播的录制回调: {}", liveRecordCallbackEntity.toString());
        if (!Strings.isNullOrEmpty(liveRecordCallbackEntity.getStream()) && !Strings.isNullOrEmpty(liveRecordCallbackEntity.getAssetId())) {

            // 判断回调状态
            // 录播记录完成
            if (RECORD_FILE_COMPLETE.equals(liveRecordCallbackEntity.getEventType())) {

                // 根据stream查询直播
                PlatformLivetelecast platformLivetelecast = platformLivetelecastMapper.getByStreamName(
                        liveRecordCallbackEntity.getStream()
                );
                if (platformLivetelecast == null) {
                    log.error("华为云直播的录制回调-直播密钥不存在: {}", liveRecordCallbackEntity.getStream());
                    return false;
                }

                // 查询对应的媒质是否存在
                LivetelecastRecord record = livetelecastRecordMapper.getByAssetId(liveRecordCallbackEntity.getAssetId());
                if (record != null) {
                    log.warn("华为云直播的录制回调-媒质已存在: {}", liveRecordCallbackEntity.getAssetId());
                    return false;
                }

                // 保存对应的媒质
                LivetelecastRecord saveRecord = new LivetelecastRecord();
                BeanUtils.copyProperties(liveRecordCallbackEntity, saveRecord);
                saveRecord.setStreamName(liveRecordCallbackEntity.getStream());
                saveRecord.setLivetelecastId(platformLivetelecast.getId());
                saveRecord.setDocentId(platformLivetelecast.getDocentId());
                saveRecord.setRecordStartTime(liveRecordCallbackEntity.getStartTime());
                saveRecord.setRecordEndTime(liveRecordCallbackEntity.getEndTime());
                saveRecord.setCreateTime(null);
                saveRecord.setUpdateTime(null);

                livetelecastRecordMapper.insert(saveRecord);
                log.info("华为云直播的录制回调-直播{}的媒质{}已保存成功", saveRecord.getStreamName(), saveRecord.getAssetId());
                return true;
            } else if (RECORD_FAILED.equals(liveRecordCallbackEntity.getEventType())) {
                // 录播记录失败
                log.error("华为云直播的录制回调-录播出错：{}, {}", liveRecordCallbackEntity.getErrorMessage(), liveRecordCallbackEntity.toString());
            }
        } else {
            log.error("华为云直播的录制回调-推流为空：{}", liveRecordCallbackEntity.toString());
        }
        return false;
    }
}
